// const // npm i express
const express = require('express')
const cors = require('cors')
const AlipaySdk = require('alipay-sdk').default
const AlipayFormData = require('alipay-sdk/lib/form').default

// 创建 alipaySdk
const alipaySdk = new AlipaySdk({
    appId: '2021000121677272', // appId 在自己的支付宝开发平台获取
    gateway: 'https://openapi.alipaydev.com/gateway.do', // 支付宝网关 ** 加dev为测试
    // 支付宝生成密钥
    privateKey:
        'MIIEogIBAAKCAQEAlur1uh9uIs17tTtvGfJjf9tWqu4PEKXffN9GRDdTxpidTJuT/BagV1m7nfupLZVwsvvBBZPVUgiJDuFTVYT1qFGqspVl3cm3KchF/muQuvwwFlGLPUF5DjCl/C1M/gmLAE19q/qI8QKc0MBunrcW31836HZu4M1x+CsgNy3sS7ktIMmVytKA8TDLPHArgr0uGNEIqLBiIsTfrDrMfB1QO7LvmzKj9N2pZEr9LN8/YWZwDn8fepxyKpU1ImNGCxzAVj7rangS7d1OJhb3GRoKQ7banuG6OLFygMEI0BzjvRtEKKcPSku4QCWs+et9/V4PX5OVFl3R4LlSgNtLqeAWZwIDAQABAoIBAFBRpguzdIsjLyaKBhwaC2hdlKeN4QisLuAneB8QDw8P5A+EcWLlq9oxUI/JAwXWlc87+0N7pTluf3si4pYOGCvHowA+UKOUGl4rT00TdEh/tRv7tA4azVGHBJ/MgEBnVcoIc8zWnpEeIdVbxMPFj2+8HmYiuen8jR5V3WRglXTKvDkoenEzZMDOQcnhPkJZuMQxWAMH0aL293RF227L0N66tOejXLtosR2vOpoSKAS53zAJbMHz/2lZJanYy+zcv5k3zq4Z5grbOmHY0Y1UxrhWSScDNywEjnLpQmteZ/vXQBrweyKvBYBGylMopLLu2odTMddTBXNmGEbTAhgJK9ECgYEAz4oiwVgzNk0MiT6QFSFs3IE/wFxxmXBYhjyslwqdc4wltlxqoMkR4VrqlY/2Hg8q3ueUKK+i1DY3Sk7HEhCVyH2xjQV4F7EJ+u3kXdr9AUgPuii1m/azyjfP0SgnoO6xATuge9NS8fXPptUYIpz+OCm+l8FCjZnocWC162XiyQ8CgYEAuig06BdNquRbMreEYnsPuLUjYHjQGrZAB4femEoCoL0eUvQbCxH6tRNVfvNq7I6pMVr+3QVC15VBZTGGa6ypxPmXpv/GQQC71D4rsGXtvOn4t9ynPznPIpqxix47CVmB29xpZ2zx1HQnwBgxvRmMyQz/A+esVjRPA4q1W9bl7SkCgYA4HWl4TZjLPdn94eSjUf5VQL00DcrJag8FfLJNxJj9/Q7VV/ecscEb5bE370zko8U7r/HnblXgeyhH9zTsNX/KTfIL50apYT0Jk/kDR4rRJeLautcf7HyAWMbgDrNoWCGT68Ngt6rdsVEMEuILudci6GaYMbEFCHBI95U4qLvBEQKBgFknhmcdOgI9SXwZrhpoK5hfkCjU033niPTSWoX+9BE9YbeC1yzPGI3+S3T4aV4P2PIdTXBW47NR9TbcqC8LS2ddzgaZ+r4I9uvoNo5N0rUmLQsJb0P1kMFW6u7gtI9+wZWlkwmIgK0qUBtMuw2Ang9v3PZmzv+eH7VnigIJAqc5AoGAU97VZNsE/mQ9BghoADZd6Jli/+pDqmnhvwB96SkKb4WHw2GsKIKLWb72MzLAKivfRvgXQ3+oOZ3nJOmSYfEKAZv/vH6t9DMuqcS0csxe01a6fM9zBeUk9YeYqEcA1YPbcypxuY/ya76eE/ceJaRJvhGV4Z5AeBlca98fZ1sfmjQ='
})

// 创建服务器
const app = express()
app.use(cors()) // 允许跨域

// 接口
app.get('/test', (req, res) => {
    res.send({
        code: 200,
        message: 'express 搭建成功'
    })
})

// 生成支付接口
app.get('/pay', (req, res) => {
    const id = new Date() * 1
    const formData = new AlipayFormData()
    // 调用 setMethod 并传入 get，会返回可以跳转到支付页面的 url
    formData.setMethod('get')

    formData.addField('notifyUrl', 'http://www.com/notify')
    formData.addField('bizContent', {
        outTradeNo: id, // 订单号 每次调用都得更改
        productCode: 'FAST_INSTANT_TRADE_PAY', // 商品付款码
        totalAmount: '100', // 订单金额
        subject: '丁士臣',
        body: '丁士臣'
    })

    alipaySdk.exec('alipay.trade.page.pay', {}, { formData }).then((resp) => {
        res.send({
            code: 200,
            message: '支付成功',
            result: resp // result 为可以跳转到支付链接的 url
        })
    })
})

// 开启服务器
app.listen(9000, () => {
    console.log('服务器启动成功')
})
